﻿using System;
using System.Net;
using Imgur.Net.Auth;
using NUnit.Framework;
using TestableHttpWebResponse;

namespace Imgur.Net.Test.ImageProvider
{
    [TestFixture]
    public class GetImageInfoTests
    {
        
        private const string imageId = "jZVF8HD";

        private const string clientId = "b56c4ebcd84a2c5";
        private const string clientSecret = "5b83ef6e0100a5a8544cc03f2e49c470d7f7cb2f";
        private const string baseTestUriTemplate = "test://api.imgur.com/3/image";

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            WebRequest.RegisterPrefix("test", TestableWebRequestCreateFactory.GetFactory());
        }

        [Test]
        public void TestGetBasicImageInfo()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);
            Net.ImageProvider imgProvider = new DefaultImageProvider(authProvider, baseTestUriTemplate);

            var expectedRequest = new TestableWebRequest(new Uri(string.Format(baseTestUriTemplate + "/{0}", imageId)));
            expectedRequest.EnqueueResponse(HttpStatusCode.OK, "Success", ResourceLoader.GetResourceText("Resources/ImageProvider/ImageModel.basic.json"), false);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRequest);

            ImageInfo info = imgProvider.GetImageInfo(imageId);
            Assert.IsNotNull(info);
            Assert.AreEqual("SbBGk", info.Id);
            Assert.IsNull(info.Title);
            Assert.AreEqual(new DateTime(2012, 07, 06, 00, 06, 33), info.TimeStamp);
            Assert.AreEqual("image/jpeg", info.MimeType);
            Assert.AreEqual(false, info.IsAnimated);
            Assert.AreEqual(2559, info.Width);
            Assert.AreEqual(1439, info.Height);
            Assert.AreEqual(521916, info.FileSize);
            Assert.AreEqual(1, info.Views);
            Assert.AreEqual(521916, info.Bandwidth);
            Assert.AreEqual("eYZd3NNJHsbreD1", info.DeleteHash);
            Assert.IsNull(info.Section);
            Assert.AreEqual("http://i.imgur.com/SbBGk.jpg", info.Link);
        }

        [Test]
        public void TestGetFullImageInfo()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);
            Net.ImageProvider imgProvider = new DefaultImageProvider(authProvider, baseTestUriTemplate);

            var expectedRequest = new TestableWebRequest(new Uri(string.Format(baseTestUriTemplate + "/{0}", imageId)));
            expectedRequest.EnqueueResponse(HttpStatusCode.OK, "Success", ResourceLoader.GetResourceText("Resources/ImageProvider/ImageModel.full.json"), false);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRequest);

            ImageInfo info = imgProvider.GetImageInfo(imageId);
            Assert.IsNotNull(info);
            Assert.AreEqual("SbBGk", info.Id);
            Assert.AreEqual("A title", info.Title);
            Assert.AreEqual("A description", info.Description);
            Assert.AreEqual(new DateTime(2012, 07, 06, 00, 06, 33), info.TimeStamp);
            Assert.AreEqual("image/jpeg", info.MimeType);
            Assert.AreEqual(false, info.IsAnimated);
            Assert.AreEqual(2559, info.Width);
            Assert.AreEqual(1439, info.Height);
            Assert.AreEqual(521916, info.FileSize);
            Assert.AreEqual(1, info.Views);
            Assert.AreEqual(521916, info.Bandwidth);
            Assert.AreEqual("eYZd3NNJHsbreD1", info.DeleteHash);
            Assert.AreEqual("cats", info.Section);
            Assert.AreEqual("http://i.imgur.com/SbBGk.jpg", info.Link);
            Assert.AreEqual(true, info.IsFavorite);
            Assert.AreEqual(true, info.IsNSFW);
            Assert.AreEqual("http://imgur.com/users/12345", info.AccountUrl);
        }

        [Test]
        public void TestGetNullAccountUrlImageInfo()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);
            Net.ImageProvider imgProvider = new DefaultImageProvider(authProvider, baseTestUriTemplate);

            var expectedRequest = new TestableWebRequest(new Uri(string.Format(baseTestUriTemplate + "/{0}", imageId)));
            expectedRequest.EnqueueResponse(HttpStatusCode.OK, "Success", ResourceLoader.GetResourceText("Resources/ImageProvider/ImageModel.nullAccountUrl.json"), false);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRequest);

            ImageInfo info = imgProvider.GetImageInfo(imageId);
            Assert.IsNotNull(info);
            Assert.AreEqual("SbBGk", info.Id);
            Assert.IsNull(info.Title);
            Assert.IsNull(info.Description);
            Assert.AreEqual(new DateTime(2012, 07, 06, 00, 06, 33), info.TimeStamp);
            Assert.AreEqual("image/jpeg", info.MimeType);
            Assert.AreEqual(false, info.IsAnimated);
            Assert.AreEqual(2559, info.Width);
            Assert.AreEqual(1439, info.Height);
            Assert.AreEqual(521916, info.FileSize);
            Assert.AreEqual(1, info.Views);
            Assert.AreEqual(521916, info.Bandwidth);
            Assert.AreEqual("eYZd3NNJHsbreD1", info.DeleteHash);
            Assert.IsNull(info.Section);
            Assert.AreEqual("http://i.imgur.com/SbBGk.jpg", info.Link);
            Assert.AreEqual(false, info.IsNSFW);
            Assert.IsNull(info.AccountUrl);
        }

        [Test]
        public void TestGetNullNSFWImageInfo()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);
            Net.ImageProvider imgProvider = new DefaultImageProvider(authProvider, baseTestUriTemplate);

            var expectedRequest = new TestableWebRequest(new Uri(string.Format(baseTestUriTemplate + "/{0}", imageId)));
            expectedRequest.EnqueueResponse(HttpStatusCode.OK, "Success", ResourceLoader.GetResourceText("Resources/ImageProvider/ImageModel.nullNSFW.json"), false);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRequest);

            ImageInfo info = imgProvider.GetImageInfo(imageId);
            Assert.IsNotNull(info);
            Assert.AreEqual("SbBGk", info.Id);
            Assert.IsNull(info.Title);
            Assert.IsNull(info.Description);
            Assert.AreEqual(new DateTime(2012, 07, 06, 00, 06, 33), info.TimeStamp);
            Assert.AreEqual("image/jpeg", info.MimeType);
            Assert.AreEqual(false, info.IsAnimated);
            Assert.AreEqual(2559, info.Width);
            Assert.AreEqual(1439, info.Height);
            Assert.AreEqual(521916, info.FileSize);
            Assert.AreEqual(1, info.Views);
            Assert.AreEqual(521916, info.Bandwidth);
            Assert.AreEqual("eYZd3NNJHsbreD1", info.DeleteHash);
            Assert.IsNull(info.Section);
            Assert.IsNull(info.IsNSFW);
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void TestNullIdGetImageInfo()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);
            Net.ImageProvider imgProvider = new DefaultImageProvider(authProvider, baseTestUriTemplate);

            var expectedRequest = new TestableWebRequest(new Uri(string.Format(baseTestUriTemplate + "/{0}", imageId)));
            expectedRequest.EnqueueResponse(HttpStatusCode.OK, "Success", ResourceLoader.GetResourceText("Resources/ImageProvider/ImageModel.full.json"), false);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRequest);

            ImageInfo info = imgProvider.GetImageInfo(null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void TestEmptyImageIdGetImageInfo()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);
            Net.ImageProvider imgProvider = new DefaultImageProvider(authProvider, baseTestUriTemplate);

            var expectedRequest = new TestableWebRequest(new Uri(string.Format(baseTestUriTemplate + "/{0}", imageId)));
            expectedRequest.EnqueueResponse(HttpStatusCode.OK, "Success", ResourceLoader.GetResourceText("Resources/ImageProvider/ImageModel.full.json"), false);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRequest);

            ImageInfo info = imgProvider.GetImageInfo(string.Empty);
        }
    }
}
